#include "pch.h"
#include "packet_receiver.h"
#ifndef CODE_INLINE
#include "packet_receiver.inl"
#endif

#include "include/address.h"
/*#include "include/bundle.h"*/
#include "include/channel.h"
#include "include/endpoint.h"
/*#include "network/event_dispatcher.h"*/
#include "include/network_interface.h"
#include "include/event_poller.h"

//-------------------------------------------------------------------------------------
PacketReceiver::PacketReceiver() :
	pEndpoint_(NULL),
	pNetworkInterface_(NULL)
{
}

//-------------------------------------------------------------------------------------
PacketReceiver::PacketReceiver(EndPoint & endpoint,
	   NetworkInterface & networkInterface	) :
	pEndpoint_(&endpoint),
	pNetworkInterface_(&networkInterface)
{
}

//-------------------------------------------------------------------------------------
PacketReceiver::~PacketReceiver()
{
}

//-------------------------------------------------------------------------------------
int PacketReceiver::handleInputNotification(int fd)
{
	if (this->processRecv(/*expectingPacket:*/true))
	{
		while (this->processRecv(/*expectingPacket:*/false))
		{
			/* pass */;
		}
	}

	return 0;
}

//-------------------------------------------------------------------------------------
Reason PacketReceiver::processPacket(Channel* pChannel, Packet * pPacket)
{
	if (pChannel != NULL)
	{
		pChannel->onPacketReceived(pPacket->length());

// 		if (pChannel->pFilter())
// 		{
// 			return pChannel->pFilter()->recv(pChannel, *this, pPacket);
// 		}
	}

	return this->processFilteredPacket(pChannel, pPacket);
}

//-------------------------------------------------------------------------------------
// EventDispatcher & PacketReceiver::dispatcher()
// {
// 	return this->pNetworkInterface_->dispatcher();
// }

//-------------------------------------------------------------------------------------
Channel* PacketReceiver::getChannel()
{
	return pNetworkInterface_->findChannel(pEndpoint_->socket());
}


